<?php
import("ORG.Util.QPage"); 
import("ORG.Util.Function");

class SupplierAction extends CommonAction {
	
	public function _initialize(){
		parent::_initialize();
	}
	//construct 
	public function __construct(){
		parent::__construct(); //must have
		$user_name          =   $_SESSION['user_name'];
		$this->assign("username",$user_name);
		$this->assign("web_title","后台管理");
	}
	
	public function _empty(){
		$this->redirect("/supplier/supplier_list");
	}
	
	public function supplier_list(){		
		$supplier_list		=	M("Suppliers")->order("suppliers_id asc")->where("is_check=1")->select();
		//t($supplier_list);		
		$this->assign("supplier_list",$supplier_list);
		$this->display();
	}
	
	public function add_supplier(){			
		$this->assign("act","add_supplier_ok");
		$this->display();
	}
	
	public function add_supplier_ok(){
		$supplier_name		=	$_REQUEST['suppliers_name'];
		if(empty($supplier_name)){
			$this->error("提供商的名称不能为空");
		}
		M('Suppliers')->create();
		$id	=	M('Suppliers')->add();
		
		if($id){
			$this->success("操作成功",U('supplier/supplier_list'));
		}
		else{
			$this->error("操作失败",U('supplier/supplier_list'));
		}
	}
	
	public function del_supplier(){
		$id		=	intval($_REQUEST['id']);
		if(empty($id)){
			$this->redirect('/supplier/supplier_list', null,2,'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">非法操作');
		}	
		/*删除*/
		M('Suppliers')->where("suppliers_id = '{$id}'")->delete();
		$this->success("操作成功",U('supplier/supplier_list'));
	}
	
	public function mod_supplier(){
		$id			=	intval($_REQUEST['id']);
		if(empty($id)){
			$this->redirect('/supplier/supplier_list', null,2,'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">非法操作');
		}			
		$supplier	=	M('Suppliers')->where("suppliers_id = '{$id}'")->find();
		
		$this->assign("act",		"mod_supplier_ok");
		$this->assign("supplier",	$supplier);
		$this->display('add_supplier');
	}
	
	public function mod_supplier_ok(){
		$supplier_id	=	intval($_REQUEST['id']);
		$supplier_name	=	trim($_REQUEST['suppliers_name']);
		if($supplier_name != $_REQUEST['old_suppliers_name']){
			$is_exsists		=	M("Suppliers")->where("suppliers_name='{$supplier_name}' and suppliers_id !='{$supplier_id}'")->find();
			if($is_exsists){
				$this->error("提供商的名称不能重复");
			}	
		}
		
		if(empty($supplier_id)){
			$this->redirect('/supplier/supplier_list', null,2,'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">非法操作');
		}		
		
		$datas			=	M("Suppliers")->create();
		unset($datas['suppliers_id']);
		M("Suppliers")->where("suppliers_id='{$supplier_id}'")->save($datas);
		$this->success("操作成功",U('supplier/supplier_list'));
	}
	
	
	
	
	
}